﻿using SD.Common;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;

namespace SlamDunk.MES.Client.ViewModels.StockInOrder
{
    /// <summary>
    /// 单据入库审核单据ViewModel
    /// </summary>
    public class CheckViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 库存单据服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IStockOrderContract> _iStockOrderContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public CheckViewModel(ServiceProxy<IStockOrderContract> iStockOrderContract)
        {
            this._iStockOrderContract = iStockOrderContract;
        }

        #endregion

        #region 属性
        #region 单据编号
        /// <summary>
        /// 单据编号
        /// </summary>
        [DependencyProperty]
        public string Number { get; set; }
        #endregion

        #region 单据类型
        /// <summary>
        /// 单据类型
        /// </summary>
        [DependencyProperty]
        public string StockOrderClass { get; set; }
        #endregion

        #region 储存位置类型
        /// <summary>
        /// 储存位置类型
        /// </summary>
        [DependencyProperty]
        public string StorageLocationType { get; set; }
        #endregion

        #region 储存位置
        /// <summary>
        /// 储存位置
        /// </summary>
        [DependencyProperty]
        public string StorageLocation { get; set; }
        #endregion

        #region 源单据类型
        /// <summary>
        /// 源单据类型
        /// </summary>
        [DependencyProperty]
        public string SourceOrderType { get; set; }
        #endregion

        #region 源单据信息
        /// <summary>
        /// 源单据信息
        /// </summary>
        [DependencyProperty]
        public string SourceOrder { get; set; }
        #endregion

        #region 描述
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion

        #region 入库单明细列表
        /// <summary>
        /// 入库单明细列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<StockOrderDetailInfo> StockOrderDetailInfoList { get; set; }
        #endregion

        #region 工厂模型
        /// <summary>
        /// 工厂模型
        /// </summary>
        [DependencyProperty]
        public string FacilityName { get; set; }
        #endregion

        #region 审核状态列表
        /// <summary>
        /// 审核状态列表
        /// </summary>
        [DependencyProperty]
        public IDictionary<bool, string> ExamineTypes { get; set; }
        #endregion

        #region 审核意见
        /// <summary>
        /// 审核意见
        /// </summary>
        [DependencyProperty]
        public string CheckedRemark { get; set; }
        #endregion

        #region 入库单Info
        /// <summary>
        /// 入库单Info
        /// </summary>
        [DependencyProperty]
        public StockInOrderInfo _StockInOrderInfo { get; set; }
        #endregion
        #endregion

        #region 方法
        #region 加载 —— async Task Load(AddViewModel addViewModel)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(StockInOrderInfo stockInOrderInfo)
        {
            _StockInOrderInfo = stockInOrderInfo;
            ExamineTypes = new Dictionary<bool, string>();
            ExamineTypes.Add(true, "审核通过");
            ExamineTypes.Add(false, "审核不通过");
            this.Number = stockInOrderInfo.Number;
            this.StorageLocationType = EnumExtension.GetEnumMember(stockInOrderInfo.TargetStorageLocationType);
            this.StorageLocation = stockInOrderInfo.TargetStorageLocationName;
            if (stockInOrderInfo.SourceOrderType != null)
            {
                this.SourceOrderType = EnumExtension.GetEnumMember(stockInOrderInfo.SourceOrderType);
            }
            this.SourceOrder = stockInOrderInfo.SourceOrderName;
            this.Description = stockInOrderInfo.Description;
            this.FacilityName = stockInOrderInfo.FacilityInfo?.Name;
            IEnumerable<StockOrderDetailInfo> StockInOrderInfos = await Task.Run(() => this._iStockOrderContract.Channel.GetStockOrderDetails(stockInOrderInfo.Id));
            StockOrderDetailInfoList = new ObservableCollection<StockOrderDetailInfo>(StockInOrderInfos);

            PageModel<StockOrderClassInfo> StockOrderClassInfos = await Task.Run(() => this._iStockOrderContract.Channel.GetStockOrderClassesByPage(
                null,//关键字
                StockDirectionType.Input,//出/入库方向类型
                null,//是否生成条码
                stockInOrderInfo.FacilityId,//工厂模型Id
                1, //pageIndex
                int.MaxValue));
            this.StockOrderClass = StockOrderClassInfos.Datas.Where(x => x.Id == stockInOrderInfo.StockOrderClassId).FirstOrDefault().Name;
        }
        #endregion

        #region 通过 —— async void Approve()
        /// <summary>
        /// 通过
        /// </summary>
        public async void Approve()
        {
            this.Busy();

            await Task.Run(() => this._iStockOrderContract.Channel.CheckStockOrder(_StockInOrderInfo.Id, true, CheckedRemark));
            await base.TryCloseAsync(true);

            this.Idle();
        }
        #endregion

        #region 拒绝 —— async void Reject()
        /// <summary>
        /// 拒绝
        /// </summary>
        public async void Reject()
        {
            this.Busy();

            await Task.Run(() => this._iStockOrderContract.Channel.CheckStockOrder(_StockInOrderInfo.Id, false, CheckedRemark));
            await base.TryCloseAsync(true);

            this.Idle();
        }
        #endregion

        #endregion
    }
}
